<?php
namespace app\admin\controller;
use app\service\apiService;
use think\Controller;
use think\facade\Db;

ini_set('max_execution_time','0');

class Task extends Controller{
    public function yzl(){
        $data['release_day'] = date("Ymd");
        $data['deadline'] = date("Ymd",strtotime("+3years",time()));
        var_dump($data);
    }
    public function test(){
        $out_room_id="202007220933363QQYAAHY";//20190704104041UE3FSYMH
		$info = Db::table("roominfo")->where('out_room_id',$out_room_id)->find();
		
		echo "<pre>";
		//var_dump($info);
		$token = (new billService())->billinfoToken($info["community_id"]);
		$api = new apiService(config('app')['roombilllist'], 'https://openapi.alipay.com/gateway.do');
		$res = $api->roomBillList(json_encode(['community_id' =>  $info['community_id'], 'out_room_id' => $info['out_room_id'],'batch_id' => "", 'bill_status' => 'WAIT_PAYMENT',  'cost_type' => '' , 'acct_period' => '' , 'release_day' => '' ,'token' => $token]));
		var_dump($res);
    }
    public function billquery(){
        
    }
    
    //收款统计
    public function statis(){
        $communitylist=Db::table("community")->select()->toArray();
        $gmt_payment_start=date("Y-m-d",strtotime("-1 day"));
        $gmt_payment_end=date("Y-m-d");
        foreach ($communitylist as $community){
            $res=Db::table("statis")->where("community_id",$community["community_id"])->where("day",$gmt_payment_start)->find();
            if(empty($res)){
                $info["community_id"]=$community["community_id"];
                $info["community_name"]=$community["community_name"];
                
                $info["alipay"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','支付宝')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["wechat"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','微信')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["pos"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','POS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["cash"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','现金')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["money"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["roomnumber"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->count();
                $info["day"]=$gmt_payment_start;
                Db::table("statis")->insert($info);
            }
            
        }
    }
    
     //收款统计
    public function statismonth(){
        $communitylist=Db::table("community")->select()->toArray();
        $gmt_payment_start=date("Y-m",strtotime("-1 month"));
        $gmt_payment_end=date("Y-m");
        foreach ($communitylist as $community){
            $res=Db::table("statis_month")->where("community_id",$community["community_id"])->where("month",$gmt_payment_start)->find();
            if(empty($res)){
                $info["community_id"]=$community["community_id"];
                $info["community_name"]=$community["community_name"];
                
                $info["alipay"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','支付宝')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["wechat"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','微信')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["pos"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','POS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["cash"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('pay_type','现金')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["money"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->sum('bill_entry_amount');
                $info["roomnumber"]=Db::name('bill')
                    ->where('community_id',$community["community_id"] )
                    ->where('status','TRADE_SUCCESS')
                    ->where('gmt_payment' , '>=' , $gmt_payment_start)
                    ->where('gmt_payment' , '<=' , $gmt_payment_end)
                    ->count();
                $info["month"]=$gmt_payment_start;
                Db::table("statis_month")->insert($info);
            }
            
        }
    }
}